
*** Bunching Analysis: Regression Results *** 

	**********------------------------------**********
	* Contents [Step 1 & 2 done in bunching_1_bins]
		* Step 1: Create Grid 
			* define cells of width a_bin = 0.5kg and eei_bin = 0.5 units in EEI. 
			* fillin to add all potential cells for each country & month 
		* Step 2: Prepare Data for regression (commented out)
			* generate outcome variables at cell level 
			* collapse at bin-country-month level
			* fillin to balance panel
	*********** Regression do-file starts here! 
		* Step 3: Baseline Regression 
			* bunching windows of 0.5 / 1.0 / 1.5 / 2.0 width 
			* outcome: levels, log + 1, ihs 
			* estimates store B* 
		* Step 4: Interaction Terms 
			* bunching windows of 0.5 / 1.0 / 1.5 / 2.0 width 
			* outcome: levels, log + 1, ihs 
			* estimates store I*
		* Step 5: Export tables to .tex format  	 
	**********------------------------------**********

capture log close 
clear all
set more off 

* Set directory
global server "R:\WSV2\TBu_AKe" 
cd "$server" // set working directory 

capture mkdir Bunching_NEW
global store "${server}\Bunching_NEW"

*Define sub directories
global desktop "C:\Users\hy65byfe\Desktop\smerge_0712"
*cd "$desktop" // set working directory 

cd "$store"
log using bunching_2_regress_2703, replace

cd // display in log file 


use bunching_2_collapse_1512 // load data created in bunching_1_bins.do  

** ----------------------------------**
* Prepare for regression
** ----------------------------------**

   ** reduce file size 
keep if year == 2011 | year == 2014 
drop if ccode == 0 

	** time structure: cut down to two periods: 2011. vs. 2014  
gen postban = 0 if year == 2011 
replace postban = 1 if year == 2014 
codebook eei_bin // 260 values 
tab postban, missing  // no missings. 

   ** outcome transformations   
gen log_units = ln(cell_date_sales)   // at monthly frequency 

foreach varx of varlist csd ccd {
   gen     log_`varx'=log(1+`varx') // log + 1 
   gen     ihs_`varx'=`varx'+(`varx'^2+1)^0.5 // inverse hyperbolic sine 
   replace ihs_`varx'=log(ihs_`varx')
	}
 

	** bunching segments : 4 different widths
	
gen bunch_W05 = 0  
replace bunch_W05 = 1 if eei_bin > 58 & eei_bin < 59

gen bunch_W1 = 0  
replace bunch_W1 = 1 if eei_bin > 57.5 & eei_bin < 59
	
gen bunch_W15 = 0  
replace bunch_W15 = 1 if eei_bin > 57.0 & eei_bin < 59  
	
gen bunch_W2 = 0  
replace bunch_W2 = 1 if eei_bin > 56.5 & eei_bin < 59
	

	** polynomials of EEI, in bins   
gen eei_2 = eei_bin^2
gen eei_3 = eei_bin^3
	 


*******------------------------***
* Bunching regressions *
*******------------------------***

			
		**********************************************************
		** Step 3: Bunching in the EEI (Table 2, upper panel)   **
		**********************************************************
		
		
		** W = 0.5 
	
foreach varx of varlist csd log_csd ihs_csd log_units {
		
		** Square 
regress `varx' 			i.bunch_W05			 ib3.ccode eei_bin eei_2 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode i.month     						  if  postban == 1  & restricted != 1,   vce(cluster eei_bin)

estimates store B05square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W05  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode i.month     if  postban == 1 & restricted != 1,   vce(cluster eei_bin)

estimates store B05cube_`varx'

}
	
		** W = 1.0 

foreach varx of varlist csd log_csd ihs_csd log_units {
		
		** Square 
regress `varx' 			i.bunch_W1			 ib3.ccode eei_bin eei_2 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode i.month     						  if  postban == 1  & restricted != 1,   vce(cluster eei_bin)

estimates store B1square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W1  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode i.month     if  postban == 1 & restricted != 1,   vce(cluster eei_bin)

estimates store B1cube_`varx'

}


		** W = 1.5 
	
foreach varx of varlist csd log_csd ihs_csd log_units {
		
		** Square 
regress `varx' 			i.bunch_W15			 ib3.ccode eei_bin eei_2 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode i.month     						  if  postban == 1  & restricted != 1,   vce(cluster eei_bin)

estimates store B15square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W15  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode i.month     if  postban == 1 & restricted != 1,   vce(cluster eei_bin)

estimates store B15cube_`varx'

}

		** W = 2.0 
	
foreach varx of varlist csd log_csd ihs_csd log_units {
		
		** Square 
regress `varx' 			i.bunch_W2			 ib3.ccode eei_bin eei_2 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode i.month     						  if  postban == 1  & restricted != 1,   vce(cluster eei_bin)

estimates store B2square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W2  		ib3.ccode eei_bin eei_2 eei_3 c.eei_bin#ib3.ccode c.eei_2#ib3.ccode c.eei_3#ib3.ccode i.month     if  postban == 1 & restricted != 1,   vce(cluster eei_bin)

estimates store B2cube_`varx'

}

	*********************
	** Report Results ***
	*********************
		
	** Cubic

esttab B05cube*, 	b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	 	nogap mtitles replace

esttab B1cube*, 	b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 		nogap mtitles replace

esttab B15cube*, 	b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 		nogap mtitles replace

esttab B2cube*, 	b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 		nogap mtitles replace


	** Square

esttab B05square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B1square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B15square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B2square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace
	

		*******************************************************************
		**  Step 4: Displaced vs. Inframarginal  (Table 2, lower panel)  **
		*******************************************************************

		** W = 0.5 
	
foreach varx of varlist csd log_csd ihs_csd log_units {
			** Square 
regress `varx' 			i.bunch_W05##i.postban c.eei_bin#i.postban c.eei_2#i.postban eei_bin eei_2												ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I05square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W05##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3			 			ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I05cube_`varx'

}
	
		** W = 1.0 
	
foreach varx of varlist csd log_csd ihs_csd log_units {
			** Square 
regress `varx' 			i.bunch_W1##i.postban c.eei_bin#i.postban c.eei_2#i.postban eei_bin eei_2												ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I1square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W1##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3			 			ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I1cube_`varx'

}

		** W = 1.5 
	
foreach varx of varlist csd log_csd ihs_csd log_units {
			** Square 
regress `varx' 			i.bunch_W15##i.postban c.eei_bin#i.postban c.eei_2#i.postban eei_bin eei_2												ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I15square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W15##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3			 			ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I15cube_`varx'

}

		** W = 2.0 
	
foreach varx of varlist csd log_csd ihs_csd log_units {
			** Square 
regress `varx' 			i.bunch_W2##i.postban c.eei_bin#i.postban c.eei_2#i.postban eei_bin eei_2	ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I2square_`varx'
		
		** Cubic  	
regress `varx' 			i.bunch_W2##i.postban c.eei_bin#i.postban c.eei_2#i.postban c.eei_3#i.postban eei_bin eei_2 eei_3	ib3.ccode i.month     if  postban != . & restricted != 1,   vce(cluster eei_bin )

estimates store I2cube_`varx'

}
	
	********************
	** Report results **
	********************
	
	
	** Displacement, report results **

	** Cubic

esttab I05cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 		nogap mtitles replace

esttab I1cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 			nogap mtitles replace

esttab I15cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 		nogap mtitles replace

esttab I2cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 			nogap mtitles replace


	** Square

esttab I05square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab I1square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab I15square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab I2square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace


	
	** Bunching, report results **

	** Cubic

esttab B05cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab B1cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab B15cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab B2cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace


	** Square

esttab B05square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B1square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B15square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B2square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace


*log close 
*log using results_summary_1512_cluster_ccode, append 

	**** Results summary for convenience ****
	


	********************
	** Report results **
	********************
	
	
	** Bunching, report results **

	** Cubic

esttab B05cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab B1cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab B15cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab B2cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace


	** Square

esttab B05square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B1square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B15square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab B2square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 *.ccode) 		nogap mtitles replace


	** Displacement, report results **

	** Cubic

esttab I05cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab I1cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab I15cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace

esttab I2cube*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode) 	nogap mtitles replace


	** Square

esttab I05square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab I1square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab I15square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace

esttab I2square*, b(%9.3f) se(%9.3f) noconstant stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* 1.postban 1.postban eei_bin eei_2 *.ccode) 		nogap mtitles replace


	*************************************
	*** Step 5: Export to .tex format ***
	*************************************
	
#delimit ; 	
esttab B05cube_log_csd B05cube_ihs_csd B1cube_log_csd B1cube_ihs_csd B15cube_log_csd B15cube_ihs_csd B2cube_log_csd B2cube_ihs_csd,
		b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) order(1.bunch* eei_bin eei_2 eei_3 *.ccode)		
		constant nogap mtitles replace;
		
#delimit ; 	
esttab B05square_log_csd B05square_ihs_csd B1square_log_csd B1square_ihs_csd B15square_log_csd B15square_ihs_csd B2square_log_csd B2square_ihs_csd,
		b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 	 *.ccode) order(1.bunch* eei_bin eei_2 *.ccode)		
		constant nogap mtitles replace;
		
#delimit ; 	
esttab I05square_log_csd I05square_ihs_csd I1square_log_csd I1square_ihs_csd I15square_log_csd I15square_ihs_csd I2square_log_csd I2square_ihs_csd,
		b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(*.bunch* *.postban eei_bin eei_2 *.ccode) order(*.bunch* *.postban eei_bin eei_2 *.ccode)		
		constant nogap mtitles replace;
		
#delimit ; 	
esttab I05cube_log_csd I05cube_ihs_csd I1cube_log_csd I1cube_ihs_csd I15cube_log_csd I15cube_ihs_csd I2cube_log_csd I2cube_ihs_csd,
		b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(*.bunch* *.postban eei_bin eei_2 eei_3 *.ccode) order(*.bunch* 1.postban eei_bin eei_2 eei_3 *.ccode)		
		constant nogap mtitles replace;
		
		
#delimit ; 	
esttab B05cube_log_csd B05cube_ihs_csd B1cube_log_csd B1cube_ihs_csd B15cube_log_csd B15cube_ihs_csd B2cube_log_csd B2cube_ihs_csd  using bunch_table41_BS.tex, b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) order(1.bunch* eei_bin eei_2 eei_3 *.ccode) nogap mtitles title(Bunching upper table\label{bunch1_cube}) replace; #delimit cr 

#delimit ; 	
esttab I05cube_log_csd I05cube_ihs_csd I1cube_log_csd I1cube_ihs_csd I15cube_log_csd I15cube_ihs_csd I2cube_log_csd I2cube_ihs_csd  using bunch_table42_BS.tex, b(%9.3f) se(%9.3f) stats(r2 N) star(* 0.10 ** .05 *** .01) keep(1.bunch* eei_bin eei_2 eei_3 *.ccode) order(1.bunch* eei_bin eei_2 eei_3 *.ccode) nogap mtitles title(Bunching Lower table\label{bunch1_cube}) replace; #delimit cr 

log close 

exit 

clear 
